package com.isoftstone;

import com.obs.services.ObsClient;
import com.obs.services.model.ObjectListing;
import com.obs.services.model.ObsObject;
import com.sun.org.apache.xerces.internal.xs.datatypes.ObjectList;

import java.io.*;
import java.util.List;

public class Main {
    public static void main(String[] args) throws FileNotFoundException{
        String endPoint = "obs.cn-south-1.myhuaweicloud.com";
        String ak = "XCWPVINCQUKNG44DJXTV";
        String sk = "deORBq4sevl5nHWFnBQStCGkNW8HbjCOL4ypZPUv";
        String backetName = "myjava";

        ObsClient obsClient = new ObsClient(ak,sk,endPoint);
        try( FileInputStream fis = new FileInputStream("E:\\Pictures\\扩展程序.png");
        ){
            obsClient.putObject(backetName,"chengxu.jpg",fis);
        }catch (IOException e){
            e.printStackTrace();
        }
//
//        try(FileOutputStream fileOutputStream = new FileOutputStream("E:\\Pictures\\扩展程序.png");){
//            ObsObject obsObject = obsClient.getObject(backetName, "chengxu.jpg");
//            InputStream inputStream = obsObject.getObjectContent();
//            byte[] block = new byte[1024];
//            int lengh = 0;
//            while((lengh = inputStream.read(block))> 0){
//                fileOutputStream.write(block,0,lengh);
//                fileOutputStream.flush();
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        obsClient.deleteObject(backetName, "chengxu.jpg");

        ObjectListing objectListing = obsClient.listObjects(backetName);
        List<ObsObject> obsObjects = objectListing.getObjects();
        for (ObsObject obsObject: obsObjects){
            System.out.println(obsObject.getObjectKey());
            System.out.println(obsObject.getMetadata().getContentLength()/1024f);
            System.out.println(obsObject.getMetadata().getLastModified());
        }
    }
}
